package mo.offer_version2;

public class L58_1 {

    public String reverseWords(String s) {
        if (s == null) {
            return null;
        }
        String[] arr = s.split(" ");
        StringBuilder builder = new StringBuilder();
        for (int i = arr.length-1; i >= 0; i--) {
            if (arr[i].length() > 0) {
                builder.append(arr[i]);
                builder.append(' ');
            }
        }
        if (builder.length() <= 1) {
            return builder.toString();
        }
        return builder.substring(0, builder.length()-1);
    }

    public static void main(String[] args) {
        L58_1 test = new L58_1();
        System.out.println(test.reverseWords("  a good    example "));
    }
}
